// Copyright (c) 2022, yuxinyong and contributors
// For license information, please see license.txt
/* eslint-disable */

frappe.query_reports["Work Order Material Availability"] = {
	onload: function(report) {
		//let filters = report.get_filter_values();
		if (frappe.model.can_write("Work Order")){
			report.page.add_inner_button(__("Reschedule Work Order"), function() {					
				let data = report.data.filter((d,i)=>d.reschedule_date);
				if (data && data.length){			
					const rows = report.datatable.datamanager._filteredRows;			
					if (rows) data = data.filter((d,i) => i in rows);
					const names = data.map(d => d.ref_docname);			
					frappe.confirm(__("Do you want to reschedule selected work orders?"), function() {
						frappe.call({
							method:'zelin_am.agile_manufacturing.report.work_order_material_availability.work_order_material_availability.reschedule_work_order',
							args: {
								data: data
							},
							freeze: true,
							freeze_message:__('Running...')}).then((r)=>{
								if(r.message){
									frappe.msgprint(r.message);
									frappe.route_options = {"name": ["in", names]}						
									frappe.set_route('List', 'Work Order');
								}
						});
					});
				}			
			});
		}
	},
	"filters": [
		{
			"fieldname":"company",
			"label": __("Company"),
			"fieldtype": "Link",
			"options": "Company",
			"reqd": 1,
			"default": frappe.defaults.get_user_default("Company")
		},
		{
			"fieldname":"production_item",
			"label": __("Production Item"),
			"fieldtype": "Link",
			"options": "Item"
		},
		{
			"fieldname":"wo_names",
			"label": __("Work Order"),
			"fieldtype": "MultiSelectList",
			"options": "Work Order",
			"get_data": function(txt) {
				let company = frappe.query_report.get_filter_value('company');
				return frappe.db.get_link_options("Work Order", txt,
					{
						"docstatus": 1,
						"status": ["not in",['Closed','Completed','Cancelled','On Hold']],
						"company": company
					});
			},
		},
		{
			"fieldname":"from_date",
			"label": __("From Date"),
			"fieldtype": "Date",
			"default": frappe.datetime.get_today(),
			"width": "80"
		},
		{
			"fieldname":"to_date",
			"label": __("To Date"),
			"fieldtype": "Date",
			"default": frappe.datetime.add_months(frappe.datetime.get_today(), 1)
		},
		{
			"fieldname":"only_actual_stock",
			"label": __("Only Actual Stock"),
			"fieldtype": "Check",
			"default": 0
		},
		{
			"fieldname":"only_short_qty_item",
			"label": __("Only Short Qty Item"),
			"fieldtype": "Check",
			"default": 0
		},
		{
			"fieldname":"include_confirmed_delivery",
			"label": __("Only Confirmed PO"),
			"fieldtype": "Check",
			"default":1
		},
		{
			"fieldname":"fully_availability_priority",
			"label": __("Fully Availability Priority"),
			"fieldtype": "Check"
		},
		{
			"fieldname":"hide_order_lines",
			"label": __("Hide Detail"),
			"fieldtype": "Check"
		}
	],
	"formatter": function(value, row, column, data, default_formatter) {
		value = default_formatter(value, row, column, data);

		if (column.fieldname == "parent_item_name" && data && data.open_qty > data.available_qty ) {
			value = `<div style="color:red">${value}</div>`;
		}

		if (column.fieldname == "production_item" && !data.name ) {
			value = "";
		}

		if (column.fieldname == "item_name" && data && data.qty > data.allotted_qty ) {
			value = `<div style="color:red">${value}</div>`;
		}
		return value;
	},
};
let fixed_trans = [
	__("Work Order and Subcontract PO"),
	__("Subcontract PO"),
	__("No Data")
]
